package tienmanh.gs.goldminer.scene;

import java.util.Random;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.util.GLState;

import tienmanh.gs.goldminer.base.BaseScene;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.manager.SceneManager.SceneType;
import tienmanh.gs.goldminer.util.Map;
import tienmanh.gs.goldminer.util.Value;


import android.R.menu;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;



public class MainMenuScene extends BaseScene implements IOnMenuItemClickListener
{
	//
	private MenuScene menuChildScene;
	
	private final int MENU_PLAY = 0;
	private final int MENU_TOP = 1;
	private final int MENU_TT = 2;
	private final int MENU_INFOR = 3;
	private final int MENU_GAME = 4;
	private final int MENU_SETTING = 5;
	private int check;
	
	public ITiledTextureRegion ho_region;
	public BitmapTextureAtlas ho_TextureAtlas;
	
	  private HUD cameraHUD;
	
	@Override
	public void createScene()
	{
		camera.setCenter(800/2, 480/2);
//		createSoundBackground();
		createBackground();
		createMenuChildScene();
	}

	public void createSoundBackground() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onBackKeyPressed()
	{
	if(check==0){
		   check = 1;
		   resourcesManager.activity.toastOnUIThread("Bạn hãy ấn back lần nữa để thoát!!");
		   engine.runOnUpdateThread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(1000);
					check = 0;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		});
	   }
	   else{
			System.exit(0);
	   }
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneType.SCENE_MENU;
	}
	

	@Override
	public void disposeScene()
	{
		// TODO Auto-generated method stub
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch(pMenuItem.getID())
		{
			case MENU_PLAY:
				SceneManager.getInstance().loadMapScene(engine);
				return true;
			case MENU_GAME:
				return true;
			case MENU_SETTING:
				return true;
			case MENU_TT:
				return true;
			case MENU_INFOR:
				return true;
			default:
				return false;
		}
	}
	
	
	public void createBackground()
	{
		cameraHUD = new HUD();
		cameraHUD.attachChild(new Sprite(0, 0, resourcesManager.textureRegionMenuBackground, vbom)
		{
    		@Override
            protected void preDraw(GLState pGLState, Camera pCamera) 
    		{
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
		});
		camera.setHUD(cameraHUD);
	}
	
	public void createMenuChildScene()
	{
		
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);
		
		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, resourcesManager.textureRegionPlay, vbom), 1.6f, 1);
//		final IMenuItem archiementMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_TT, resourcesManager.archie_region, vbom), 1.6f, 1);
//		final IMenuItem topMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_TOP, resourcesManager.top_region, vbom), 1.6f, 1);
		final IMenuItem optionMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_SETTING, resourcesManager.textureRegionVolume, vbom), 1.6f, 1);
		final IMenuItem moreMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_GAME, resourcesManager.textureRegionMore, vbom), 1.6f, 1);
		final IMenuItem inforMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_INFOR, resourcesManager.textureRegionInfor, vbom), 1.6f, 1);
		
		menuChildScene.addMenuItem(playMenuItem);
//		menuChildScene.addMenuItem(archiementMenuItem);
//		menuChildScene.addMenuItem(topMenuItem);
		menuChildScene.addMenuItem(optionMenuItem);
		menuChildScene.addMenuItem(moreMenuItem);
		menuChildScene.addMenuItem(inforMenuItem);
		
		
		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);
		
		playMenuItem.setPosition(625, 380);
		inforMenuItem.setPosition(520, 385);
		moreMenuItem.setPosition(430, 385);
		optionMenuItem.setPosition(340, 385);
		
		
		
		menuChildScene.setOnMenuItemClickListener(this);
		
		 cameraHUD.setChildScene(menuChildScene);
	}
	
	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
//		System.exit(0);
	}
	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}
	
}